package com.itsv.cyzx.db;

import java.util.ArrayList;

import com.itsv.cyzx.bean.ChatMsgEntity;
import com.itsv.cyzx.bean.TWBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DingCanDB extends SQLiteOpenHelper {

	SQLiteDatabase db = null;
	private static final int dbVersion = 1;
	private Context context = null;
	private static String dbName = "itsv_dingcan.db";

	public DingCanDB(Context context) {
		super(context, dbName, null, dbVersion);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

		/**
		 * ��Ϣ��
		 */
		String tablesql = "create table Tab_DDList(id TEXT , title TEXT ,jiage TEXT,"
				+ "title_pic TEXT,shuliang TEXT,time TEXT)";
		db.execSQL(tablesql);
		tablesql = "create table Tab_WWCList(id TEXT , title TEXT ,jiage TEXT,"
				+ "title_pic TEXT,shuliang TEXT,time TEXT)";
		db.execSQL(tablesql);
		tablesql = "create table Tab_dingdan(time TEXT)";
		db.execSQL(tablesql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		if (newVersion <= oldVersion) {
			return;
		} else {
			context.deleteDatabase(dbName);
			onCreate(db);
		}
	}

	/**
	 * 
	 * 
	 * @return ��ȡȫ����Ϣ�б����
	 */
	public String getDingdan() {
		String time = null;
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		String sql = "select * from Tab_dingdan";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			time = cursor.getString(cursor.getColumnIndex("time"));
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return time;
	}

	/**
	 * �����Ϣ
	 * 
	 * @param bean
	 */
	public void insertDingdan(String time) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("time", time);
		db.insert("Tab_dingdan", null, cv);
	}

	/**
	 * ��Ϣɾ��
	 * 
	 * @param id
	 *            :ɾ������ ���Ϊnull ���ȫ��ɾ��
	 * @return
	 */
	public void deleteDingdan(String time) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		if (time == null) {
			db.delete("Tab_dingdan", null, null);
		} else {
			db.delete("Tab_dingdan", "time=?", new String[] { time });
		}
	}

	/**
	 * ͨ��id��ѯ��Ϣ
	 * 
	 * @return
	 */
	public ArrayList<TWBean> getDDlist() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ArrayList<TWBean> list = new ArrayList<TWBean>();
		String sql = "select * from Tab_DDList ";
		Cursor cursor = db.rawQuery(sql, null);
		TWBean bean = null;
		while (cursor.moveToNext()) {
			bean = new TWBean();
			bean.setId(cursor.getString(cursor.getColumnIndex("id")));
			bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			bean.setJiage(cursor.getString(cursor.getColumnIndex("jiage")));
			bean.setTitle_pic(cursor.getString(cursor
					.getColumnIndex("title_pic")));
			bean.setShuliang(cursor.getString(cursor.getColumnIndex("shuliang")));
			bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(bean);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return list;
	}

	/**
	 * �����Ϣ
	 * 
	 * @param bean
	 */
	public void insertDDList(TWBean bean) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("id", bean.getId());
		cv.put("title", bean.getTitle());
		cv.put("jiage", bean.getJiage());
		cv.put("title_pic", bean.getTitle_pic());
		cv.put("shuliang", bean.getShuliang());
		cv.put("time", bean.getTime());
		db.insert("Tab_DDList", null, cv);
	}

	/**
	 * ��Ϣɾ��
	 * 
	 * @param id
	 *            :ɾ������ ���Ϊnull ���ȫ��ɾ��
	 * @return
	 */
	public void deleteDDListData() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		db.delete("Tab_DDList", null, null);
	}

	/**
	 * ͨ��id��ѯ��Ϣ
	 * 
	 * @return
	 */
	public ArrayList<TWBean> getWWClist() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ArrayList<TWBean> list = new ArrayList<TWBean>();
		String sql = "select * from Tab_WWCList ";
		Cursor cursor = db.rawQuery(sql, null);
		TWBean bean = null;
		while (cursor.moveToNext()) {
			bean = new TWBean();
			bean.setId(cursor.getString(cursor.getColumnIndex("id")));
			bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			bean.setJiage(cursor.getString(cursor.getColumnIndex("jiage")));
			bean.setTitle_pic(cursor.getString(cursor
					.getColumnIndex("title_pic")));
			bean.setShuliang(cursor.getString(cursor.getColumnIndex("shuliang")));
			bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(bean);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return list;
	}

	/**
	 * �����Ϣ
	 * 
	 * @param bean
	 */
	public void insertWWCList(TWBean bean) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("id", bean.getId());
		cv.put("title", bean.getTitle());
		cv.put("jiage", bean.getJiage());
		cv.put("title_pic", bean.getTitle_pic());
		cv.put("shuliang", bean.getShuliang());
		cv.put("time", bean.getTime());
		db.insert("Tab_WWCList", null, cv);
	}

	/**
	 * ��Ϣɾ��
	 * 
	 * @param id
	 *            :ɾ������ ���Ϊnull ���ȫ��ɾ��
	 * @return
	 */
	public void deleteWWCData() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		db.delete("Tab_WWCList", null, null);
	}

	/**
	 * �ر���ݿ�
	 */
	public void closeDB() {
		if (db != null) {
			db.close();
			db = null;
		}
	}
}
